﻿Public Class FMNilai

    'member variables
    Private _cnilai As CNilaiSiswa
    Private _semesterAktif As tbl_semester
    Private _csemester As CSemester
    Private _mataPelajaran As tbl_mata_pelajaran
    Private _kelas As tbl_kelas
    Private _cPembagianKelas As CPembagianKelas

    'forms
    Private WithEvents _flSemester As FLSemester
    Private WithEvents _flKelas As FLKelas
    Private WithEvents _flMP As FLMataPelajaran


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles LabelJudul.MouseDown, PanelJudul.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp, PanelJudul.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove, PanelJudul.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub


    Private Sub Panel1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles PanelJudul.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(cont As Control, g As Graphics, p As Pen)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
            ElseIf TypeOf ctl Is Panel Then
                PaintTextBox(ctl, g, p)
            ElseIf TypeOf ctl Is GroupBox Then
                PaintTextBox(ctl, g, p)
            End If
        Next
    End Sub

    Private Sub SetRegionTextBox(cont As Control)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            End If
        Next
    End Sub

    Private Sub btnMaximize_Click(sender As System.Object, e As System.EventArgs) Handles btnMaximize.Click

        WindowState = FormWindowState.Maximized
        btnMinimize.Visible = False
        btnMaximize.Visible = False
        btnClose.Visible = False
    End Sub

    Private Sub btnMinimize_Click(sender As System.Object, e As System.EventArgs) Handles btnMinimize.Click
        WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub FSizeChanged(sender As Object, e As System.EventArgs) Handles Me.SizeChanged
        If WindowState <> FormWindowState.Maximized Then
            btnMinimize.Visible = True
            btnMaximize.Visible = True
            btnClose.Visible = True
        End If
    End Sub
    '-----------------------------------------------------------------------------------------------
#End Region

    Private Sub btnKeluar_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'clear datasource
        DGMaster.Columns.Clear()
        DGMaster.DataSource = Nothing
        lblKosong.Visible = True

        'generate column
        _cnilai = New CNilaiSiswa

        'kolom nilai
        Dim dgCol As DataTable = _cnilai.getPenilaian(_semesterAktif.id, _kelas.id, _mataPelajaran.id)
        

        If dgCol.Rows.Count > 0 Then
            'hilangkan label info kosong
            lblKosong.Visible = False

            'kolom no
            Dim cNo As New DataGridViewTextBoxColumn
            With cNo
                .Name = "CNo"
                .HeaderText = "No"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            End With
            DGMaster.Columns.Add(cNo)

            'column nama
            Dim cNama As New DataGridViewTextBoxColumn
            With cNama
                .Name = "cNama"
                .HeaderText = "Nama"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            End With
            DGMaster.Columns.Add(cNama)

            'column cidsiswa
            Dim CIDSiswa As New DataGridViewTextBoxColumn
            With CIDSiswa
                .Name = "cidsiswa"
                .HeaderText = "cidsiswa"
                .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                .Visible = False
            End With
            DGMaster.Columns.Add(CIDSiswa)

            Dim _nilCont As Integer = 1
            Dim _listKat As New List(Of Integer)
            'Dim _list

            For Each aRow As DataRow In dgCol.Rows
                Dim aCol As New DataGridViewTextBoxColumn
                With aCol
                    .Tag = aRow.Item("id") 'aRow.Item("kat_nil")
                    .Name = IIf(aRow.Item("kat_nil") = 3, "N" & _nilCont, IIf(aRow.Item("kat_nil") = 2, "UTS", "UAS"))
                    .HeaderText = IIf(aRow.Item("kat_nil") = 3, "N" & _nilCont, IIf(aRow.Item("kat_nil") = 2, "UTS", "UAS"))
                    _nilCont += 1
                    .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
                End With
                DGMaster.Columns.Add(aCol)
            Next

            'add column rapor
            Dim totCol As New DataGridViewTextBoxColumn
            totCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            totCol.HeaderText = "Rapor"
            'totCol.DefaultCellStyle.BackColor = Color.Orange
            DGMaster.Columns.Add(totCol)

            'add column filler
            Dim filCol As New DataGridViewTextBoxColumn
            filCol.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            DGMaster.Columns.Add(filCol)

            'tampilkan data siswa
            Dim dTabSiswa As DataTable
            _cPembagianKelas = New CPembagianKelas
            _cnilai = New CNilaiSiswa
            dTabSiswa = _cPembagianKelas.getDistribusiKelas(_kelas.id, _semesterAktif.id)
            For Each asiswa As DataRow In dTabSiswa.Rows
                Dim rind As Integer = DGMaster.Rows.Add()
                'set nama
                DGMaster.Rows(rind).Cells("cidsiswa").Value = asiswa.Item("id_ssw")
                DGMaster.Rows(rind).Cells("cnama").Value = asiswa.Item("siswa")
                'set nilai
                For Each aCol As DataGridViewColumn In DGMaster.Columns
                    If IsNumeric(aCol.Tag) Then
                        DGMaster.Rows(rind).Cells(aCol.Index).Value = _cnilai.getNilaiSiswa(asiswa.Item("id_ssw"), aCol.Tag, _mataPelajaran.id)
                    End If
                Next
            Next
        Else
            'tampilkan label kosong
            lblKosong.Visible = True
            MessageBox.Show("Tidak ada data yang ditampilkan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If



    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1
    End Sub

    Private Sub tbMataPelajaran_Enter(sender As Object, e As System.EventArgs) Handles tbMataPelajaran.Enter, tbMataPelajaran.Click
        'tampilkan mata pelajaran
        _flMP = New FLMataPelajaran
        _flMP.ShowDialog()
    End Sub

    Private Sub _flMP_DataTerpilih() Handles _flMP.DataTerpilih
        _mataPelajaran = _flMP.getSelected
        tbMataPelajaran.Text = _mataPelajaran.nama
    End Sub

    Private Sub _flKelas_DataTerpilih() Handles _flKelas.DataTerpilih
        _kelas = New tbl_kelas
        With _kelas
            .id = _flKelas.SelectedID
            .nama = _flKelas.SelectedNama
        End With
        tbKelas.Text = _kelas.nama
        'clear mata pelajaran
        tbMataPelajaran.Clear()
        'clear datasource
        DGMaster.Columns.Clear()
        DGMaster.DataSource = Nothing
    End Sub

    Private Sub tbKelas_Enter(sender As Object, e As System.EventArgs) Handles tbKelas.Enter, tbKelas.Click
        'tampilkan form kelas
        _flKelas = New FLKelas
        _flKelas.ShowDialog()
    End Sub

    Private Sub tbSemester_Enter(sender As Object, e As System.EventArgs) Handles tbSemester.Enter, tbSemester.Click
        'tampilkan form list semester
        _flSemester = New FLSemester
        _flSemester.ShowDialog()
    End Sub

    Private Sub _flSemester_DataTerpilih() Handles _flSemester.DataTerpilih
        _semesterAktif = New tbl_semester
        With _semesterAktif
            .id = _flSemester.SelectedIDSemester
            .nama = _flSemester.SelectedNamaSemester
        End With
        tbSemester.Text = _semesterAktif.nama

        'clear kelas & mata pelajaran
        tbKelas.Clear()
        tbMataPelajaran.Clear()
        'clear datasource
        DGMaster.Columns.Clear()
        DGMaster.DataSource = Nothing
    End Sub

    Private Sub FMNilai_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set semester
        _csemester = New CSemester
        _semesterAktif = New tbl_semester
        With _semesterAktif
            .id = _csemester.getSemesterAktif.Rows(0).Item("id")
            .nama = _csemester.getSemesterAktif.Rows(0).Item("nama")
        End With
        tbSemester.Text = _semesterAktif.nama
    End Sub
End Class